<?php

namespace app\admin\model;

use think\Model;

/**
 * 社群推荐
 * @package app\admin\model
 */
class ResourceRecommend extends Model
{
    /**
     * 单社群推荐
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findResourceRecommend($where)
    {
        $info = self::where($where)->find();
        return $info ? $info->toArray() : [];
    }

    /**
     * 添加社群推荐
     * @param $data
     * @return ResourceRecommend|Model
     */
    public static function createResourceRecommend($data)
    {
        return self::create($data);
    }

    /**
     * 推荐列表
     * @param $where
     * @param $where1
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getRecommendList($where, $where1, $page, $pagesize)
    {
        $data = self::alias('re')
            ->join('resource r', 'r.id = re.rid')
            ->join('resource_cate c', 'c.id = r.cids', 'left')
            ->join('location l', 'l.location_id = r.city', 'left')
            ->field('re.rid,re.status,re.type,re.add_time,re.end_time,re.show_time,r.id,r.name,r.founder,r.province,r.city,r.area,r.uid,r.desc,r.qrcode,r.logo,r.company,r.create_time,r.is_high,r.is_service,r.number,r.is_partner,c.name as cate_name,l.name as city_name')
            ->where($where)
            ->where($where1['otherWheres'])
            ->where([['r.status', '<>', -1], ['re.type', '=', 2]])
            ->page($page, $pagesize)
            ->order($where1['order'])
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 统计推荐列表
     * @param $where
     * @return mixed
     */
    public static function countRecommendList($where)
    {
        return self::alias('re')
            ->join('resource r', 'r.id = re.rid')
            ->where($where)
            ->where('r.status', '<>', -1)
            ->count();
    }

    /**
     * 删除推荐列表
     * @param $where
     * @return bool
     */
    public static function delResourceRecommend($where)
    {
        return self::where($where)->delete();
    }

}